'Approval
Option Strict Off
imports System
imports System.Collections
imports PHSRAG.Insight21.Humans
imports PHSRAG.Insight21.BusinessObjects
imports FormsManagement = PHSRAG.Insight21.FormsManagement


module Script
   sub Main()

      Dim questionnaireAnswers As new ArrayList()
      Dim qV As FormsManagement.QuestionnaireVersion
      Dim questionnaireAnswer As FormsManagement.QuestionnaireAnswer

      questionnaireAnswers= currentProtVersion.QuestionnaireAnswers

      if "PPTYPE_AM" = currentProcess.Type then
	
		For Each questionnaireAnswer In questionnaireAnswers
		
			qV  = questionnaireAnswer.QuestionnaireVersion
			
			If (qV.Questionnaire.Name = "Amendment") Then
				Dim answers as new ArrayList()
				
				' Set the title if it's been changed.
				answers = questionnaireAnswer.GetAnswers("Title") 
				
				if answers.Count > 0 then
					Dim titleAnswer as FormsManagement.Answer = answers(0)
					if titleAnswer.Value = "Title" then
						answers = nothing
						answers = questionnaireAnswer.GetAnswers("AmendTitle") 
						Dim newTitle as FormsManagement.Answer = answers(0)
						rexParams.NewProtocolTitle = newTitle.Value
					end if
				end if
					
				' Set the Version if it's been changed.
				answers = nothing
				answers = questionnaireAnswer.GetAnswers("Version") 
				
				if answers.Count > 0 then
					Dim versionAnswer as FormsManagement.Answer = answers(0)
					if versionAnswer.Value = "Version" then
						answers = nothing
						answers = questionnaireAnswer.GetAnswers("AmendVersion") 
						Dim newVersion as FormsManagement.Answer = answers(0)
						rexParams.NewProtocolVersion = newVersion.Value
					end if
				end if
				
			end if
		
		Next
      end if
      
      if "ANIMALPPTYPE_AM" = currentProcess.Type then
		Dim numberOfAnimals as Integer
		Dim catB as Integer
		Dim catC as Integer
		Dim catD as Integer
		Dim catE as Integer

		For Each questionnaireAnswer In questionnaireAnswers
		
			qV  = questionnaireAnswer.QuestionnaireVersion
			
			If (qV.Questionnaire.Name.ToUpper() = "IACUCAMENDMENT") Then
				Dim answers as new ArrayList()
				
				' Set the title if it's been changed.
				answers = questionnaireAnswer.GetAnswers("ComTitle") 
				if answers.Count > 0 then
					Dim titleAnswer as FormsManagement.Answer = answers(0)
					if titleAnswer.Value = "Y" then
						answers = nothing
						answers = questionnaireAnswer.GetAnswers("NewTitle") 
						Dim newTitle as FormsManagement.Answer = answers(0)
						rexParams.NewProtocolTitle = newTitle.Value
					end if
				end if
				
				answers = questionnaireAnswer.GetAnswers("CatB") 
				if answers.Count > 0 then
				    if IsNumeric(answers(0).Value) then
						catB = answers(0).Value
				    end if 
                end if

				answers = questionnaireAnswer.GetAnswers("CatC") 
				if answers.Count > 0 then
				    if IsNumeric(answers(0).Value) then
						catC = answers(0).Value
				    end if 
                end if
                
				answers = questionnaireAnswer.GetAnswers("CatD") 
				if answers.Count > 0 then
				    if IsNumeric(answers(0).Value) then
						catD = answers(0).Value
				    end if 
                end if

				answers = questionnaireAnswer.GetAnswers("CatE") 
				if answers.Count > 0 then
				    if IsNumeric(answers(0).Value) then
						catE = answers(0).Value
				    end if 
                end if
			end if		
		    ' For Legacy amendments, the total number of animals question is on STD App
			If (questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Standard Application") Then
				Dim answers as new ArrayList()
				
				' Set the title if it's been changed.
				answers = questionnaireAnswer.GetAnswers("TotNumAnimals") 
				if answers.Count > 0 then
				    if IsNumeric(answers(0).Value) then
						numberOfAnimals = answers(0).Value
						rexParams.TotalNumberOfAnimals = numberOfAnimals
				    end if 
                end if
            end if
		Next
	
		' @@@ 5/28/2009 This is "temporary" until the forms are split out properly and the total number of animals 
		' question is on a common form used between triennial, amendment, and standard app.		
		' Set the total number of animals in the original standard app to the total number of animals from 
		' the triennial questionnaire.  
		Dim latestApp as ProtocolVersion = Protocol.GetLatestApplicationVersion(currentProcess.ProtocolId, connectionString, false, cachePolicy)
		if not latestApp is nothing then
		
			For Each questionnaireAnswer In latestApp.QuestionnaireAnswers
				If (questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Standard Application") then
					Dim answers as ArrayList = questionnaireAnswer.GetAnswers("TotNumAnimals")
					if answers.Count > 0 then
						answers(0).Value = CInt(answers(0).Value) + catB + catC + catD + catE
					end if

					answers = questionnaireAnswer.GetAnswers("CatBNum")
				    if answers.Count > 0 then
				        if IsNumeric(answers(0).Value) then
				            catB = catB + CInt(answers(0).Value)
				        end if
				        answers(0).Value = catB
                    end if

					answers = questionnaireAnswer.GetAnswers("CatCNum")
				    if answers.Count > 0 then
				        if IsNumeric(answers(0).Value) then
				            catC = catC + CInt(answers(0).Value)
				        end if
				        answers(0).Value = catC
                    end if

					answers = questionnaireAnswer.GetAnswers("CatDNum")
				    if answers.Count > 0 then
				        if IsNumeric(answers(0).Value) then
				            catD = catD + CInt(answers(0).Value)
				        end if
				        answers(0).Value = catD
                    end if

					answers = questionnaireAnswer.GetAnswers("CatENum")
				    if answers.Count > 0 then
				        if IsNumeric(answers(0).Value) then
				            catE = catE + CInt(answers(0).Value)
				        end if
				        answers(0).Value = catE
                    end if
				end if
			Next
			latestApp.Save(connectionString)
		end if
      end if

      if "ANIMALPPTYPE_TR" = currentProcess.Type then
		Dim numberOfAnimals as Integer
		Dim catB as Integer
		Dim catC as Integer
		Dim catD as Integer
		Dim catE as Integer
		For Each questionnaireAnswer In questionnaireAnswers
		
		    ' For eIACUC triennials, the total number of animals question is on TR
			If (questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Triennial Review") Then
				Dim answers as new ArrayList()
				
				' Set the title if it's been changed.
				answers = questionnaireAnswer.GetAnswers("TotNumAnimals") 
				if answers.Count > 0 then
				    if IsNumeric(answers(0).Value) then
						numberOfAnimals = answers(0).Value
				        Dim currentProtocol as Protocol = currentProcess.Protocol
				        currentProtocol.NumberOfAnimals = numberOfAnimals
				        currentProtocol.Save(connectionString)
				    end if 
                end if
            end if
		    ' For Legacy triennials, the total number of animals question is on STD
			If (questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Standard Application") Then
				Dim answers as new ArrayList()
				
				' Set the title if it's been changed.
				answers = questionnaireAnswer.GetAnswers("TotNumAnimals") 
				if answers.Count > 0 then
				    if IsNumeric(answers(0).Value) then
						numberOfAnimals = answers(0).Value
				        Dim currentProtocol as Protocol = currentProcess.Protocol
				        currentProtocol.NumberOfAnimals = numberOfAnimals
				        currentProtocol.Save(connectionString)
				    end if 
                end if
            end if
            ' on eIACUC triennial, the categories are on potential pain and distress
			If (questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Potential Pain and Distress") Then
				Dim answers as new ArrayList()
				
				answers = questionnaireAnswer.GetAnswers("CatBNum") 
				if answers.Count > 0 then
				    if IsNumeric(answers(0).Value) then
						catB = answers(0).Value
				    end if 
                end if

				answers = questionnaireAnswer.GetAnswers("CatCNum") 
				if answers.Count > 0 then
				    if IsNumeric(answers(0).Value) then
						catC = answers(0).Value
				    end if 
                end if
                
				answers = questionnaireAnswer.GetAnswers("CatDNum") 
				if answers.Count > 0 then
				    if IsNumeric(answers(0).Value) then
						catD = answers(0).Value
				    end if 
                end if

				answers = questionnaireAnswer.GetAnswers("CatENum") 
				if answers.Count > 0 then
				    if IsNumeric(answers(0).Value) then
						catE = answers(0).Value
				    end if 
                end if
            end if
        next

		' @@@ 5/28/2009 This is "temporary" until the forms are split out properly and the total number of animals 
		' question is on a common form used between triennial, amendment, and standard app.		
		' Set the total number of animals in the original standard app to the total number of animals from 
		' the triennial questionnaire.  
		Dim latestApp as ProtocolVersion = Protocol.GetLatestApplicationVersion(currentProcess.ProtocolId, connectionString, false, cachePolicy)
		if not latestApp is nothing then
		
			For Each questionnaireAnswer In latestApp.QuestionnaireAnswers
				If (questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Standard Application") then
					Dim answers as ArrayList = questionnaireAnswer.GetAnswers("TotNumAnimals")
					if answers.Count > 0 then
						answers(0).Value = numberOfAnimals
					end if
					
					answers = questionnaireAnswer.GetAnswers("CatBNum")
				    if answers.Count > 0 then
				        answers(0).Value = catB
                    end if

					answers = questionnaireAnswer.GetAnswers("CatCNum")
				    if answers.Count > 0 then
				        answers(0).Value = catC
                    end if

					answers = questionnaireAnswer.GetAnswers("CatDNum")
				    if answers.Count > 0 then
				        answers(0).Value = catD
                    end if

					answers = questionnaireAnswer.GetAnswers("CatENum")
				    if answers.Count > 0 then
				        answers(0).Value = catE
                    end if
				end if
			Next
			latestApp.Save(connectionString)
		end if		
      end if
      
      if "PPTYPE_IR" = currentProcess.Type or "PPTYPE_AM" = currentProcess.Type then

		Dim sponsorIds As new ArrayList()

		For Each questionnaireAnswer In questionnaireAnswers
			qV = questionnaireAnswer.QuestionnaireVersion
			
			If (qV.Questionnaire.Name = "Sponsor Funding") Then

				Dim sponsorType as System.String = questionnaireAnswer.GetAnswerValue("SponsorType")
		
				if sponsorType = "DepartmentFund" then
				   sponsorIds.Add(3749)
				else
				   sponsorIds.Add(questionnaireAnswer.ExternalId)
				end if
			End If
		Next

		currentProcess.Protocol.SaveSponsorMap(connectionString, sponsorIds)

      end if

   end sub	
end module